package middleware

/*
import (
	"encoding/json"
	"fmt"
	"getaway/basic/config"
	behaviorproto "getaway/basic/proto/behaviorproto"
	"github.com/gin-gonic/gin"
	"strings"
	"time"
)

// 定义页面路径常量
const (
	PageHome          = "首页"
	PageServiceSelect = "服务选择页"
	PagePayment       = "支付页"
	PageOrderDetail   = "订单详情页"
	PageDestination   = "目的地输入页"
	PageUserProfile   = "个人中心页"
)

// 点击行为类型
const (
	ClickViewCarDetail     = "查看车辆详情"
	ClickInputDestination  = "输入目的地"
	ClickSelectServiceType = "选择服务类型"
	ClickConfirmOrder      = "确认订单"
	ClickPayOrder          = "支付订单"
	ClickCancelOrder       = "取消订单"
)

// 从请求中获取或生成会话ID
func getSessionID(c *gin.Context) string {
	// 尝试从cookie中获取sessionID
	cookie, err := c.Cookie("session_id")
	if err == nil && cookie != "" {
		return cookie
	}

	// 如果没有，则生成新的sessionID
	sessionID := fmt.Sprintf("%d_%s", time.Now().UnixNano(), c.ClientIP())

	// 设置cookie
	c.SetCookie("session_id", sessionID, 86400*30, "/", "localhost", false, true)
	return sessionID
}

// 根据请求路径获取页面名称
func getPageName(path string) string {
	path = strings.ToLower(path)
	if strings.Contains(path, "home") || path == "/" {
		return PageHome
	} else if strings.Contains(path, "service") || strings.Contains(path, "select") {
		return PageServiceSelect
	} else if strings.Contains(path, "payment") || strings.Contains(path, "pay") {
		return PagePayment
	} else if strings.Contains(path, "order") && strings.Contains(path, "detail") {
		return PageOrderDetail
	} else if strings.Contains(path, "destination") {
		return PageDestination
	} else if strings.Contains(path, "profile") || strings.Contains(path, "user") {
		return PageUserProfile
	}
	return path
}

// 记录访问记录
func recordVisit(c *gin.Context, userId interface{}, userIP, sessionID, path, pageName string) {
	// 异步记录，不阻塞主流程
	go func() {
		// 构建请求
		req := &behaviorproto.RecordVisitReq{
			RequestId: sessionID,
			UserId:    getUserIdInt64(userId),
			Path:      path,
			Method:    c.Request.Method,
			Ip:        userIP,
			UserAgent: c.Request.UserAgent(),
			Referer:   c.Request.Referer(),
			DeviceId:  c.Request.UserAgent(), // 使用UserAgent作为设备标识
			VisitTime: time.Now().UnixNano() / int64(time.Millisecond),
		}

		// 调用行为服务记录访问
		// 注意：这里假设behavior服务已经创建并连接
		// 在实际项目中，需要确保BehaviorClient已经初始化
		if config.BehaviorClient != nil {
			_, err := config.BehaviorClient.RecordVisit(c, req)
			if err != nil {
				fmt.Printf("记录访问失败: %v\n", err)
			}
		} else {
			// 如果没有连接到behavior服务，打印日志
			fmt.Printf("记录访问: 用户ID=%v, IP=%s, 会话ID=%s, 页面=%s\n", userId, userIP, sessionID, pageName)
		}
	}()
}

// 记录点击行为
func RecordClickBehavior(c *gin.Context, userId interface{}, behaviorType string, details map[string]interface{}) {
	sessionID := getSessionID(c)
	path := c.Request.URL.Path
	pageName := getPageName(path)

	// 异步记录，不阻塞主流程
	go func() {
		// 序列化details
		detailsJSON, _ := json.Marshal(details)

		// 构建请求
		req := &behaviorproto.RecordClickReq{
			RequestId:   sessionID,
			UserId:      getUserIdInt64(userId),
			ElementId:   pageName,
			ElementType: behaviorType,
			Action:      "click",
			PageUrl:     path,
			Ip:          c.ClientIP(),
			DeviceId:    c.Request.UserAgent(),
			ClickTime:   time.Now().UnixNano() / int64(time.Millisecond),
			Params:      map[string]string{"details": string(detailsJSON)},
		}

		// 调用行为服务记录点击
		// 注意：这里假设behavior服务已经创建并连接
		if config.BehaviorClient != nil {
			_, err := config.BehaviorClient.RecordClick(c, req)
			if err != nil {
				fmt.Printf("记录点击行为失败: %v\n", err)
			}
		} else {
			// 如果没有连接到behavior服务，打印日志
			fmt.Printf("记录点击行为: 用户ID=%v, 类型=%s, 页面=%s\n", userId, behaviorType, pageName)
		}
	}()
}

// 记录停留时间
func recordStayTime(c *gin.Context, userId interface{}, sessionID, path, pageName string, duration time.Duration) {
	// 异步记录，不阻塞主流程
	go func() {
		// 构建请求
		req := &behaviorproto.RecordStayTimeReq{
			RequestId:    sessionID,
			UserId:       getUserIdInt64(userId),
			PageUrl:      path,
			StayDuration: int64(duration.Milliseconds()),
			StartTime:    time.Now().Add(-duration).UnixNano() / int64(time.Millisecond),
			EndTime:      time.Now().UnixNano() / int64(time.Millisecond),
			Ip:           c.ClientIP(),
			DeviceId:     c.Request.UserAgent(),
		}

		// 调用行为服务记录停留时间
		// 注意：这里假设behavior服务已经创建并连接
		if config.BehaviorClient != nil {
			_, err := config.BehaviorClient.RecordStayTime(c, req)
			if err != nil {
				fmt.Printf("记录停留时间失败: %v\n", err)
			}
		} else {
			// 如果没有连接到behavior服务，打印日志
			fmt.Printf("记录停留时间: 用户ID=%v, 页面=%s, 时长=%dms\n", userId, pageName, duration.Milliseconds())
		}
	}()
}


}
*/
